import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:identification_photo/utils/cache_util.dart';
import 'package:identification_photo/view/home_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<CacheUtil>(
        future: buildFuture(context),
        builder: (BuildContext context, AsyncSnapshot<CacheUtil> snapshot) {
          Widget app = snapshot.connectionState == ConnectionState.done
              ? buildFinishWidget()
              : buildWaitWidget();
          return app;
        });
  }

  Widget buildFinishWidget() {
    return MaterialApp(
      title: '证件照裁切',
      //是否显示Debug Logo
      debugShowCheckedModeBanner: false,
      //easyLoading 初始化
      builder: EasyLoading.init(),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }

  Widget buildWaitWidget() {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Center(
            child: CircularProgressIndicator(),
          ),
        ),
      ),
    );
  }

  Future<CacheUtil> buildFuture(BuildContext context) async {
    //初始化SP
    CacheUtil util = await CacheUtil.perInit();

    return util;
  }
}
